<div class="layuimini-container layui-anim layui-anim-upbit">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" id="form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">选择日期</label>
                            <div class="layui-input-inline" style="width: 160px;">
                                <input type="text" name="startTime" id="date1" autocomplete="off" class="layui-input">
                            </div>
                            <label style="float: left;padding-top: 4px;padding-right: 4px;">_&nbsp;&nbsp;</label>
                            <div class="layui-input-inline" style="width: 160px;">
                                <input type="text" name="endTime" id="date2" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">标题查询</label>
                            <input type="text" id="titleQuery" name="titleQuery" autocomplete="off" class="layui-input"
                                style="width:240px">
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                lay-filter="search-notice-btn" style="margin-left:10px;width:100px;height:38px;border:none;outline:none;border-radius:6px;background-color:#007bff;color:white"><i
                                    class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <br>
        <div class="layui-btn-group">
            <button type="submit" id="addnotice" class="layui-btn layui-btn-primary" lay-filter="data-add-btn"
                style="width:90px;height:38px;background-color:#FFB800;border:none;outline:none;color:white"><i class="layui-icon layui-icon-form"></i>
                添加</button>
        </div>
        <table id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <button class="layui-btn layui-btn-xs layui-btn-normal data-count-edit" lay-event="edit">编辑</button>
            <button class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" >删除</button>
        </script>
        <div id="page"></div>
    </div>
</div>
<script>

//form表单
layui.form.render();

//开始日期
var today = new Date();
var start = layui.laydate.render({
    elem: '#date1',
    calendar: true,
    trigger: 'click',
    max: $("#date2").val(),
    istime: true,
    istoday: true,
    done: function (value, date, endDate) {
        if (value != "") {
            end.config.min = {
                year: date.year,
                month: date.month - 1,
                date: date.date,
            }
        } else {//清空以后最小值
            end.config.min = {
                year: 1970,
                month: 1,
                date: 1,
            }
        }
    }
});

//结束日期
var end = layui.laydate.render({
    elem: '#date2',
    calendar: true,
    trigger: 'click',
    max: $("#date2").val(),
    istime: true,
    istoday: true,
    done: function (value, date) {
        if (value != "") {
            start.config.max = {
                year: date.year,
                month: date.month - 1,
                date: date.date,
            }
        } else {//清空以后最大值
            start.config.max = {
                year: today.getFullYear(),
                month: today.getMonth(),
                date: today.getDate()
            }
        }
    }
});

//初始化table
 var  form = layui.form,
    laydate = layui.laydate,
    table = layui.table,
    cols = [
        { align: 'center', title: '序号', type: 'numbers', width: '5%' },
        { field: 'id', title: 'ID', width: '8%', hide: true },
        { field: 'title', title: '标题', align: 'center', width: '40%' },
        { field: 'senderName', title: '发布用户', align: 'center', width: '10%' },
        { field: 'isTop', title: '是否置顶', align: 'center', width: '10%' },
        // { field: 'sortCode', title: '分类code', align: 'center', hide: true },
        // { field: 'sortShowName', title: '分类名', align: 'center', width: '10%' },
        { field: 'sendTime', title: '发布时间', align: 'center', width: '20%'},
        { field: 'text', title: '全文', align: 'center', hide: true },
        { title: '操作', templet: '#currentTableBar',align: "center"}
    ];

layuiEx.tableRender('/admin/notice/queryNotice', 'currentTableId', 'page', cols, {
    page: 1,
    pageSize: 10,
});

    
     // 监听搜索操作
     form.on('submit(search-notice-btn)', function (data) {
        startTime = $("#date1").val(),
        endTime = $("#date2").val(),
        searchValue = $("#titleQuery").val();
        layuiEx.tableRender('/admin/notice/queryNotice', 'currentTableId', 'page', cols, {
            startTime: startTime,
            endTime: endTime,
            searchValue: searchValue,
            page: 1,
            pageSize: 10
        });
        return false;
    });

       // 监听添加公告按钮
       $("#addnotice").on("click", function () {
         var content = layui.miniPage.getHrefContent('mypages/messageManagement/notice_add.html');
         var openWH = layui.miniPage.getOpenWidthHeight();
        var index = layer.open({
            title: '添加通知公告',
            type: 1,
            shade: 0.2,
            maxmin: true,
            shadeClose: true,
            zIndex:1000,
            area: ['1200px','90%'],
            // area: ['1200px','850px'],
            content: content,
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
        return false;
    });

        //监听删除和编辑操作
        layui.table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            editData = obj.data;
            opreatType = "edit";
            var content = layui.miniPage.getHrefContent('mypages/messageManagement/notice_edit.html');
            var openWH = layui.miniPage.getOpenWidthHeight();
            var index = layer.open({
                title: '编辑通知公告',
                type: 1,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                zIndex:1000,
                area: ['1200px','90%'],
                // area: ['1200px', '850px'],
                content: content,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('确定删除？', function (index) {
                deltDate = obj.data;
                shared.ajaxPost('/admin/notice/delNotice', deltDate, function (res) {
                    refreshTable();
                })
                layer.close(index);
            });
        }
    });

    //更新后刷新table
    function refreshTable() {
        cols = [
                { align: 'center', title: '序号', type: 'numbers', width: '5%' },
                { field: 'id', title: 'ID', width: '8%', hide: true },
                { field: 'title', title: '标题', align: 'center', width: '40%' },
                { field: 'senderName', title: '发布用户', align: 'center', width: '10%' },     
                { field: 'isTop', title: '是否置顶', align: 'center', width: '10%' },
                // { field: 'sortCode', title: '分类code', align: 'center', hide: true },
                // { field: 'sortShowName', title: '分类名', align: 'center', width: '10%' },
                { field: 'sendTime', title: '发布时间', align: 'center', width: '20%'},
                { field: 'text', title: '全文', align: 'center', hide: true },
                { title: '操作', templet: '#currentTableBar',align: "center"}
        ];

        layuiEx.tableRender('/admin/notice/queryNotice', 'currentTableId', 'page', cols, {
        page: 1,
        pageSize: 10,
        });
    }
       
    
 
</script>

